package com.hmall.controller.back;

import com.hmall.service.UpDownServiceImpl;

import io.swagger.annotations.Api;

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.stereotype.Controller;

import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;


@Controller
@RequestMapping("/manage/upload")
@Api(value = "图片上传API", description = "图片上传API说明")
public class UploadManageController {
    private final
    UpDownServiceImpl upDownService;

    @Autowired
    public UploadManageController(UpDownServiceImpl upDownService) {
        this.upDownService = upDownService;
    }

    @RequestMapping(value = "/testuploadimg", method = RequestMethod.POST)
    @ResponseBody
    /*public  String uploadImg(@RequestParam("file") MultipartFile file,
                             HttpServletRequest request) {*/
    public Map<String, Object> headImgUpload(HttpServletRequest request,
                                             @RequestParam("file")
                                                     MultipartFile file, String local_url) {
        Map<String, Object> value = new HashMap<>();
        value.put("success", true);
        value.put("errorCode", 0);
        value.put("errorMsg", "");

        try {
            String head = upDownService.updateHead(file);
            value.put("data", head);
        } catch (IOException e) {
            e.printStackTrace();
            value.put("success", false);
            value.put("errorCode", 200);
            value.put("errorMsg", "图片上传失败");
        } catch (Exception e) {
            e.printStackTrace();
        }
        value.put("local_url", local_url);
        return value;
    }
}
